package com.example.demo.service;

import com.example.demo.entity.User;
import com.example.mapper.UserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class ChangePasswordService {

    private static final Logger logger = LoggerFactory.getLogger(AuthService.class);
    @Autowired
    private UserMapper userMapper;

    private  final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    public boolean change(Integer jobNum, String oldPassword, String newPassword) {
        User user = userMapper.findByJobNum(jobNum);
        if (user != null && passwordEncoder.matches(oldPassword, user.getUpassword())) {
            String encryptedPassword = passwordEncoder.encode(newPassword);
            user.setUpassword(encryptedPassword);
            logger.info("开始修改密码: {}", user);
            int insert = userMapper.updatePD(encryptedPassword,jobNum);
            logger.info("用户插入数据库结果: {}", insert);
            return true;
        }else {
            return false;
        }
    }
}